using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Logging;
using ProjetoComperio.Framework.Web.Security;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;
using ProjetoComperio.SystemFramework.Web;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    [RequiresAuthentication]
    [ExceptionLogging]
    public class EducationalITApplicationController : ComperioController
    {
        private EducationalITApplicationBLL EducationalITApplicationBLL;

        public EducationalITApplicationController(EducationalITApplicationBLL educationalITApplicationBLL)
        {
            this.EducationalITApplicationBLL = educationalITApplicationBLL;
        }
        public EducationalITApplicationController()
        {
            EducationalITApplicationBLL = new EducationalITApplicationBLL();
        }

        public ActionResult Index()
        {
            ViewData["GridSource"] = EducationalITApplicationBLL.RetrieveEducationalITApplications();
            return View();
        }

        [HttpGet]
        public ActionResult MyIndex()
        {
            ViewData["GridSource"] = EducationalITApplicationBLL.RetrieveEducationalITApplications(Convert.ToInt32(this.CurrentUser.PersonID));
            return View();
        }



        #region create
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "ContribuitionId")] EducationalITApplication educationalITApplication)
        {
            if (!ModelState.IsValid)
                return View();

            AcademicPersonBLL academicPersonBLL = new AcademicPersonBLL();
            educationalITApplication.AcademicPerson = academicPersonBLL.RetrieveAcademicPersonByID(Convert.ToInt32(this.CurrentUser.PersonID));
            EducationalITApplicationBLL.Create(educationalITApplication);

            return RedirectToAction("Index");
        }
        #endregion

        #region edit

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([Bind(Exclude = "ContribuitionId")] EducationalITApplication educationalITApplication)
        {
            if (!ModelState.IsValid)
                return View(educationalITApplication);

            var id = URLEncrypt.DecryptFromString(Convert.ToString(Session["id"]));
            var originalEducationalITApplication = EducationalITApplicationBLL.RetrieveEducationalITApplication(Convert.ToInt32(id));

            originalEducationalITApplication.Title = educationalITApplication.Title;
            originalEducationalITApplication.RegisterDate = educationalITApplication.RegisterDate;
            originalEducationalITApplication.SourceInfo = educationalITApplication.SourceInfo;
            originalEducationalITApplication.IsFree = educationalITApplication.IsFree;

            originalEducationalITApplication.IsOpenSource = educationalITApplication.IsOpenSource;
            originalEducationalITApplication.UseInstructions = educationalITApplication.UseInstructions;
            originalEducationalITApplication.AdditionalInfo = educationalITApplication.AdditionalInfo;

            EducationalITApplicationBLL.Edit(originalEducationalITApplication);

            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(String id)
        {
            Session["id"] = id;
            var educationalITApplication = EducationalITApplicationBLL.RetrieveEducationalITApplication(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));
            return View(educationalITApplication);
        }
        #endregion

        [HttpGet]
        public ActionResult Visualize(string id)
        {
            var entity =
                EducationalITApplicationBLL.RetrieveEducationalITApplication(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));
            return View(entity);
        }
    }

}
